//
//  AddStyleCell.m
//  linkingGame
//
//  Created by zw wu on 15/4/25.
//  Copyright (c) 2015年 com.jinWu. All rights reserved.
//

#import "AddStyleCell.h"

@interface AddStyleCell ()

@property (nonatomic,retain) UIView *myHighlightView;
@property (nonatomic,retain) UIImageView *showImageView1;
@property (nonatomic,retain) UIImageView *showImageView2;
@property (nonatomic,retain) UIImageView *showImageView3;
@property (nonatomic,retain) UIImageView *showImageView4;

@end

@implementation AddStyleCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.layer.borderWidth = 1;
        self.layer.masksToBounds = YES;
        self.layer.borderColor = [[UIColor grayColor] CGColor];
        self.layer.cornerRadius = 3;
        
        UIView *myHighlightView = [[UIView alloc]initWithFrame:self.bounds];
        myHighlightView.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.3];
        self.myHighlightView = myHighlightView;
        [myHighlightView release];
        
        UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [self addGestureRecognizer:recognizer];
        [recognizer release];
        
        int imageWidth = round(frame.size.width / 2);
        
        UIImageView *showImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imageWidth, imageWidth)];
        [self addSubview:showImageView1];
        self.showImageView1 = showImageView1;
        [showImageView1 release];
        
        UIImageView *showImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(imageWidth, 0, imageWidth, imageWidth)];
        [self addSubview:showImageView2];
        self.showImageView2 = showImageView2;
        [showImageView2 release];
        
        UIImageView *showImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, imageWidth, imageWidth, imageWidth)];
        [self addSubview:showImageView3];
        self.showImageView3 = showImageView3;
        [showImageView3 release];
        
        UIImageView *showImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(imageWidth, imageWidth, imageWidth, imageWidth)];
        [self addSubview:showImageView4];
        self.showImageView4 = showImageView4;
        [showImageView4 release];
    }
    
    return self;
}

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    if (selected)
    {
        self.layer.borderColor = [[UIColor colorWithRed:0 green:128.0/255.0 blue:1.0 alpha:1.0] CGColor];
        
        if (self.pathId)
        {
            [[NSUserDefaults standardUserDefaults] setObject:self.pathId forKey:@"linkingGameSelectedExpression"];
        }  
    }
    else
    {
        self.layer.borderColor = [[UIColor grayColor] CGColor];
    }
}

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    if (highlighted)
    {
        [self addSubview:self.myHighlightView];
    }
    else
    {
        [self.myHighlightView removeFromSuperview];
    }
}

- (void)setPathId:(NSString *)pathId
{
    [_pathId release];
    _pathId = [pathId retain];
    
    if (self.row < KMaxLoctionExpression)
    {
        self.showImageView1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_1",pathId]];
        self.showImageView2.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_2",pathId]];
        self.showImageView3.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_3",pathId]];
        self.showImageView4.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_4",pathId]];
    }
    else
    {
        NSArray *tmplist = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathId error:nil];
        for (int i = 0;i < 4;i++)
        {
            NSString *fullPath = [pathId stringByAppendingPathComponent:[tmplist objectAtIndex:i]];
            switch (i)
            {
                case 0:
                    self.showImageView1.image = [UIImage imageWithContentsOfFile:fullPath];
                    break;
                case 1:
                    self.showImageView2.image = [UIImage imageWithContentsOfFile:fullPath];
                    break;
                case 2:
                    self.showImageView3.image = [UIImage imageWithContentsOfFile:fullPath];
                    break;
                case 3:
                    self.showImageView4.image = [UIImage imageWithContentsOfFile:fullPath];
                    break;
                default:
                    break;
            }
        }
    }
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(goToEdit:) || action == @selector(goToDelete:) ||
        action == @selector(goToShare:))
    {
        return YES;
    }
    
    return NO; //隐藏系统默认的菜单项
}

- (void)longPress:(UILongPressGestureRecognizer *)recognizer
{
    //Todo
    //if (recognizer.state == UIGestureRecognizerStateBegan && self.row >= KMaxLoctionExpression)
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        [self becomeFirstResponder];

        UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Edit"action:@selector(goToEdit:)];
        UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Delete"action:@selector(goToDelete:)];
        UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Share"action:@selector(goToShare:)];
        
        UIMenuController *menu = [UIMenuController sharedMenuController];
        [menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];
        [menu setTargetRect:self.bounds inView:self];
        [menu setMenuVisible:YES animated:YES];
    }
}

- (void)goToEdit:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(goToEdit:)])
    {
        [self.delegate goToEdit:self];
    }
}

- (void)goToDelete:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(goToDelete:)])
    {
        [self.delegate goToDelete:self];
    }
}

- (void)goToShare:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(goToShare:)])
    {
        [self.delegate goToShare:self];
    }
}

- (void)dealloc
{
    self.myHighlightView = nil;
    self.delegate = nil;
    self.pathId = nil;
    self.showImageView1 = nil;
    self.showImageView2 = nil;
    self.showImageView3 = nil;
    self.showImageView4 = nil;
    
    [super dealloc];
}

@end
